/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.contract.bean;

import com.ibatis.dao.client.DaoManager;
import com.yunhoo.checkFlow.db.dao.BaseDao;
import com.yunhoo.checkFlow.model.CheckTree;
import com.yunhoo.checkFlow.model.FlowModel;
import com.yunhoo.checkFlow.model.Level;
import com.yunhoo.checkFlow.util.BillType;
import com.yunhoo.contract.db.impl.ContractImpl;
import com.yunhoo.contract.model.MainInformation;

import com.yunhoo.sys.db.config.DaoConfig;
import com.yunhoo.sys.util.common.*;
import java.util.*;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

@ManagedBean(name = "contractCheckBean_")
@SessionScoped
public class ContractCheckBean_backup {

    private static BaseDao type;
    private MainInformation exp = new MainInformation();
    private List<MainInformation> list = new ArrayList<MainInformation>();
    private DataModel<MainInformation> model = new ListDataModel<MainInformation>();
    private List<Level> checknodes;//审批节点
    private List<CheckTree> currennodes;
    private Level currennode;//当前节点
    boolean flag = false;

    public ContractCheckBean_backup() {
        init();
        DaoManager daoManager = DaoConfig.getDaoManager();
        type = (BaseDao) daoManager.getDao(BaseDao.class);
    }

    private void init() {
        //list = IbatisTemplate.queryForList("queryForFlowCheck", null);
        list = ContractImpl.queryContractList(null);
        model.setWrappedData(list);
    }

    public void checkinfo() {
        exp = (MainInformation) model.getRowData();
        FlowModel flowmodel = new FlowModel();
        flowmodel.getNode().setId(BillType.CONTRACT_INPUT);
        flowmodel.setNumber(exp.getNumber());
        flowmodel.setCreatetime(LoginParam.getLoginDate());
        checknodes = type.queryList(flowmodel, "flowmodel");
        System.out.println("checknodes : " + checknodes.size());
        NavigationTool.getSelectedUrl("/page/contract/check/checkflowinfo.xhtml");
    }

    public void reback() {
        NavigationTool.getSelectedUrl("/page/contract/check/check.xhtml");
    }
//     public void actionForCheck(){
//        for(Level l:checknodes){
//            for(CheckTree c:l.getNodes()){
//              if(c.getName().trim().equals(login.getPer().getCpsn_name().trim())){
//                flag = true;
//                break;
//              }
//              System.out.println("c.getName():" + c.getName());
//              System.out.println("loginname :" + login.getPer().getCpsn_name());
//            }
//
//        }
//        System.out.println("flag+++ : " + flag);
//     }

    public void actionForCheck2() {
        System.out.println("actionForCheck2 !!! ");
        flag = false;
        for (CheckTree c : currennodes) {
            System.out.println("c.getName():" + c.getName());
            System.out.println("loginname :" + LoginParam.getPer().getCpsn_name());
            if (c.getName() != null && c.getName().trim().equals(LoginParam.getPer().getCpsn_name().trim())) {
                flag = true;
                break;
            }
        }
        System.out.println("flag+++ : " + flag);
    }

    //审批
    public void check() {
        Date now = LoginParam.getLoginDate();
        Date writedate = exp.getOrderdate();
        System.out.println("flag : " + flag);
        if (flag) {
            exp.setFlowMark(new Util().check(exp.getNumber(), "单据", LoginParam.getPer().getCpsn_name(), checknodes, currennode));
            //IbatisTemplate.update("updateContractFlowMark", exp);
            ContractImpl.updateContractFlowMark(exp);
        } else {
            ToolKit.msgs_err("您无权限审批");
        }
        ToolKit.msgs_err("非本期间单据不可操作");
        flag = false;
    }

    //取消审批
    public void cancelcheck() {
        Date now = LoginParam.getLoginDate();
        Date writedate = exp.getOrderdate();
        //DocumentQueryConditionBean dqcb = (DocumentQueryConditionBean) FacesUtils.getManagedBean("queryBean");       
        if (flag) {
            System.out.println("cancelcheck---------------");
            new Util().cancelcheck(exp.getNumber(), "", LoginParam.getPer().getCpsn_name(), checknodes, currennode);
            //判断下一级是否已审核
            exp.setFlowMark("1");
            //IbatisTemplate.update("updateContractFlowMark", exp);
            ContractImpl.updateContractFlowMark(exp);
        } else {
//            DocumentQueryConditionBean dqcb = (DocumentQueryConditionBean) FacesUtils.getManagedBean("queryBean");
//            dqcb.setErr(new ErrMessage("您无权限弃审！！！"));
            ToolKit.msgs_err("您无权限弃审!");
        }
        flag = false;
    }

    public MainInformation getExp() {
        return exp;
    }

    public void setExp(MainInformation exp) {
        this.exp = exp;
    }

    public List<MainInformation> getList() {
        return list;
    }

    public void setList(List<MainInformation> list) {
        this.list = list;
    }

    public DataModel<MainInformation> getModel() {
        return model;
    }

    public void setModel(DataModel<MainInformation> model) {
        this.model = model;
    }

    public List<Level> getChecknodes() {
        return checknodes;
    }

    public void setChecknodes(List<Level> checknodes) {
        this.checknodes = checknodes;
    }

    public List<CheckTree> getCurrennodes() {
        return currennodes;
    }

    public void setCurrennodes(List<CheckTree> currennodes) {
        this.currennodes = currennodes;
    }

    public Level getCurrennode() {
        return currennode;
    }

    public void setCurrennode(Level currennode) {
        this.currennode = currennode;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
